 /******************************************************************************
  * @file    PT32X005_ifmc.h
  * @author  应用开发团队
  * @version V1.1.0
  * @date    2020/1/27
  * @brief    This file contains all the functions prototypes for the FLASH firmware library.
  *
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef PT32X005_IFMC_H
#define PT32X005_IFMC_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "PT32X005.h"


/** @addtogroup FLASH
  * @{
  */

/* Exported types ------------------------------------------------------------*/


/**  @brief  FLASH Status **/
typedef enum
{
  FLASH_WRITE_END=1,
  FLASH_ERASE_END,
  FLASH_PROTECT_ERASE_END,
  FLASH_BUSY,
  FLASH_CMD_ERR,
  FLASH_KEY_ERR,
  FLASH_ADDR_ERR,
  FLASH_COMPLETE,
  FLASH_TIMEOUT
}FLASH_Status;

/* Exported constants --------------------------------------------------------*/

/** @defgroup IS_FLASH_WAIT_SEL  **/
#define IS_FLASH_WAIT_SEL(SEL)	(((SEL) == IFMC_WAIT_0) || \
                                   			((SEL) == IFMC_WAIT_1))

/** @defgroup FLASH_Interrupts  **/
#define IS_FLASH_IT(IT) ((IT<= 0x7F) && (IT!=0x08))

#define IS_FLASH_PSC_Value(Value) ((Value< 64) )

/** @defgroup FLASH_Address  **/
#define FLASH_PAGE_SIZE    ((u16)0x200)	 /*!< 512B */
#define MAINCODE_START_ADDR  ((u32)0x00000000)

#if defined(FLASH_SIZE_16K)
	#if defined(BOOT_SIZE_3K5)
		#define MAINCODE_END_ADDR    ((u32)0x00003FFF)
	#elif defined(BOOT_SIZE_2K5)
		#define MAINCODE_END_ADDR    ((u32)0x000043FF)
	#elif defined(BOOT_SIZE_1K5)
		#define MAINCODE_END_ADDR    ((u32)0x000047FF)
	#else
		#define MAINCODE_END_ADDR    ((u32)0x00004DFF)
	#endif
#elif defined(FLASH_SIZE_32K)
	#if defined(BOOT_SIZE_3K5)
		#define MAINCODE_END_ADDR    ((u32)0x00006FFF)
	#elif defined(BOOT_SIZE_2K5)
		#define MAINCODE_END_ADDR    ((u32)0x000073FF)
	#elif defined(BOOT_SIZE_1K5)
		#define MAINCODE_END_ADDR    ((u32)0x000077FF)
	#else
		#define MAINCODE_END_ADDR    ((u32)0x00007DFF)
	#endif
#else
	#define MAINCODE_END_ADDR    ((u32)0x00006FFF)
#endif

#define IS_FLASH_PROGRAM_ADDRESS(ADDRESS)   (((ADDRESS) <= MAINCODE_END_ADDR))


/** @defgroup IS_FLASH_SR_SEL & GET  **/
#define IS_FLASH_SR_SEL(SEL)	((((SEL) & (u16)0xff00) == 0) && ((SEL) != (u16)0x00))
#define IS_FLASH_SR_GET(GET)	(((GET) == IFMC_SR_WOV) || \
								((GET) == IFMC_SR_POV) || \
								((GET) == IFMC_SR_COV) || \
								((GET) == IFMC_SR_BUSY) || \
								((GET) == IFMC_SR_CERR) || \
								((GET) == IFMC_SR_KERR) || \
								((GET) == IFMC_SR_AERR))

/** @defgroup FLASH_Timeout_definition  **/
#define FLASH_ER_PRG_TIMEOUT	((u32)0x000B0000)

/** @brief  FLASH Operation Commands **/
#define FLASH_OPCMD_PRO   ((u32)0x00000000)
#define FLASH_OPMOD_RE    ((u32)0x00000000)
#define FLASH_OPCMD_PERA   IFMC_CR_CMD_PERA
#define FLASH_OPCMD_MERA   IFMC_CR_CMD_MERA
#define FLASH_OPMOD_WR    IFMC_CR_MODE
#define FLASH_OPSTR_STRT  IFMC_CR_START


/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void IFMC_SetLatency(u32 FLASH_Latency);
void IFMC_SetPrescaler(u8 Prescaler);
FLASH_Status IFMC_GetFlashStatus(void);
FLASH_Status IFMC_WaitForLastFlashOperation(u32 Timeout);

FLASH_Status IFMC_ErasePage(u32 Page_Address);
FLASH_Status IFMC_EraseAllPages(void);
FLASH_Status IFMC_ProgramWord(u32 Address, u32 Data);

u32 IFMC_ReadWord(u32 Address);
u16 IFMC_ReadHalfWord(u32 Address);
u8 IFMC_ReadByte(u32 Address);

void IFMC_ITConfig(u32 FLASH_IT, FunctionalState NewState);
FlagStatus IFMC_GetFlagStatus(u32 FLASH_FLAG);
void IFMC_ClearFlag(u32 FLASH_FLAG);
RemapStatus IFMC_GetBootStatus(void);
ProtectStatus IFMC_GetReadProtectStatus(void);

/**
  * @}
  */


#ifdef __cplusplus
}
#endif

#endif
